package com.miaomiaojia.waterfallitem;

import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import com.miaomiaojia.android.BuildConfig;
import com.miaomiaojia.android.R;
import com.miaomiaojia.waterfall.Constants;
import com.miaomiaojia.waterfall.WaterFallView;
import com.miaomiaojia.waterfallfetch.CacheFetch;

public class FlowView extends ImageView implements View.OnClickListener,
		View.OnLongClickListener {

	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);
		// 画边框 暂时去除小边框
		// Rect rec = canvas.getClipBounds();
		// rec.bottom=rec.bottom-3;
		// rec.right=rec.right-5;
		// Paint paint = new Paint();
		// paint.setColor(color);
		// paint.setStyle(Paint.Style.STROKE);
		// canvas.drawRect(rec, paint);
		// this.setPadding(2,3,6,5);
		this.setBackgroundDrawable(this.getResources().getDrawable(
				R.drawable.item));
		// LayoutParams lp = new LayoutParams(23, 23);
		// lp.setMargins(10, 0, 0, 0);
	}

	private Context context;
	public Bitmap bitmap;
	private int columnIndex;
	private int rowIndex;
	private String fileName;
	private int ItemWidth;
	private Handler viewHandler;
	// private int color;
	//private String namespace = "http://xmz.com";
	private Intent clickIntent;

	public FlowView(Context c, AttributeSet attrs, int defStyle) {
		super(c, attrs, defStyle);
		// color = Color.parseColor(attrs.getAttributeValue(namespace,
		// "BorderColor"));
		this.context = c;
		Init();
	}

	public FlowView(Context c, AttributeSet attrs) {
		super(c, attrs);
		// color = Color.parseColor(attrs.getAttributeValue(namespace,
		// "BorderColor"));
		this.context = c;
		Init();
	}

	public FlowView(Context c) {
		super(c);
		// color = Color.parseColor("#CCCCCC");
		this.context = c;
		Init();
	}

	private void Init() {
		setOnClickListener(this);
		setOnLongClickListener(this);
		setAdjustViewBounds(true);
	}

	@Override
	public boolean onLongClick(View v) {
		// Log.d("FlowView", "LongClick");
		// Toast.makeText(context, "长按" + getId(), Toast.LENGTH_SHORT).show();
		return true;
	}

	@Override
	public void onClick(View v) {
		// Log.d("FlowView", "Click");
		// Toast.makeText(context, "单击" + getId(), Toast.LENGTH_SHORT).show();
		this.context.startActivity(clickIntent);
	}

	public void LoadImage() {
		new LoadImageThread().start();
	}

	public void Reload() {
		if (this.bitmap == null) {
			new ReloadImageThread().start();
		}
	}

	public void recycle() {
		setImageBitmap(null);
		if ((this.bitmap == null) || (this.bitmap.isRecycled()))
			return;
		this.bitmap.recycle();
		this.bitmap = null;
	}

	public int getColumnIndex() {
		return columnIndex;
	}

	public void setColumnIndex(int columnIndex) {
		this.columnIndex = columnIndex;
	}

	public int getRowIndex() {
		return rowIndex;
	}

	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getItemWidth() {
		return ItemWidth;
	}

	public void setItemWidth(int itemWidth) {
		ItemWidth = itemWidth;
	}

	public Handler getViewHandler() {
		return viewHandler;
	}

	public FlowView setViewHandler(Handler viewHandler) {
		this.viewHandler = viewHandler;
		return this;
	}

	public Intent getClickIntent() {
		return clickIntent;
	}

	public void setClickIntent(Intent it) {
		this.clickIntent = it;
	}

	class ReloadImageThread extends Thread {
		@Override
		public void run() {
			File cacheFile = CacheFetch.dowanLoadBitmap(getContext(),
					getFileName());
			bitmap = BitmapFactory.decodeFile(cacheFile.getPath());
			((Activity) context).runOnUiThread(new Runnable() {
				public void run() {
					if (bitmap != null) {
						setImageBitmap(bitmap);
					} else {
						if (BuildConfig.DEBUG) {
							Log.d(WaterFallView.TAG, "bitmap->" + null);
						}
					}
				}
			});
		}
	}

	class LoadImageThread extends Thread {
		LoadImageThread() {
		}

		public void run() {
			File cacheFile = CacheFetch.dowanLoadBitmap(getContext(),
					getFileName());
			bitmap = BitmapFactory.decodeFile(cacheFile.getPath());

			((Activity) context).runOnUiThread(new Runnable() {
				public void run() {
					if (bitmap != null) {
						int width = bitmap.getWidth();
						int height = bitmap.getHeight();

						LayoutParams lp = getLayoutParams();

						// int layoutHeight = (height * getItemWidth()) / width;
						// if (lp == null) {
						// lp = new LayoutParams(getItemWidth(), layoutHeight);
						// }
						if (lp == null) {
							lp = new LayoutParams(width, height);
						}
						
						setLayoutParams(lp);

						setImageBitmap(bitmap);
						setMinimumWidth(width);
						setMinimumHeight(height);
						Handler h = getViewHandler();
						// Message m = h.obtainMessage(Constants.HANDLER_WHAT,
						// width, layoutHeight, FlowView.this);
						Message m = h.obtainMessage(Constants.HANDLER_WHAT,
								width, height, FlowView.this);
						h.sendMessage(m);
					}
				}
			});
		}
	}
}
